import 'package:flutter/material.dart';
import 'package:camera/camera.dart';
import 'package:get/get.dart';
import 'package:hibrid_flutter/camera/camera_page_controller.dart';
import 'package:hibrid_flutter/gen/assets.gen.dart';
import 'package:hibrid_flutter/util/channel_utils.dart';
import 'package:hibrid_flutter/widget/Button.dart';
import 'package:hibrid_flutter/widget/dir_image.dart';

class CameraPage extends StatefulWidget {
  @override
  _CameraPageState createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
  late CameraPageController _cameraPageController;

//  CameraController? controller;
//  late List<CameraDescription> _cameras;

  @override
  void initState() {
    super.initState();
    _cameraPageController = CameraPageController();
//    _cameraPageController = Get.put(CameraPageController());
    _cameraPageController.initCameraController();
//    _initState();
  
  }

  @override
  void dispose() {
    _cameraPageController.cameraController?.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    var childCamera = _cameraPageController.cameraController != null &&
            _cameraPageController.cameraController!.value.isInitialized
        ? CameraPreview(_cameraPageController.cameraController!)
        : Container(
            decoration: const BoxDecoration(color: Colors.black),
            width: double.infinity,
            height: double.infinity,
          );
    return Stack(
      children: [
        Container(
          decoration: const BoxDecoration(color: Colors.black),
        ),
        Obx(() => _cameraPageController.cameraController != null &&
                _cameraPageController.cameraController!.value.isInitialized
            ? CameraPreview(_cameraPageController.cameraController!)
            : Container(
                decoration: const BoxDecoration(color: Colors.black),
                width: double.infinity,
                height: double.infinity,
              )),
        Button(
          builder: (Color color) => DirImage(
            url: Assets.images.icCloseGray.path,
            width: 66,
            height: 66,
            fit: BoxFit.cover,
          ),
          onTap: _onTap,
        ),
        Positioned(top:140, child: Button(
          builder: (Color color) => DirImage(
            url: Assets.images.cO.path,
            width: 86,
            height: 86,
            fit: BoxFit.cover,
          ),
          onTap: onTapDialog,
        )),
//        GestureDetector(
//          onTap: _onTap,
//          child: DirImage(
//            url: Assets.images.icCloseGray.path,
//            width: 66,
//            height: 66,
//            fit: BoxFit.cover,
//          ),
//        ),
      ],
    );
  }

  void _onTap() {
    print('flutter-debug  _onTap');
    ChannelUtils.closeCamera();
  }

  void _initState() async {
//    _cameras = await availableCameras();
//    controller = CameraController(_cameras[0], ResolutionPreset.max);
//    controller?.initialize().then((_) {
//      if (!mounted) {
//        return;
//      }
//      setState(() {});
//    }).catchError((Object e) {
//      if (e is CameraException) {
//        switch (e.code) {
//          case 'CameraAccessDenied':
//            print('User denied camera access.');
//            break;
//          default:
//            print('Handle other errors.');
//            break;
//        }
//      }
//    });
  }

  /// 构建控制按钮=图标+文字
  Widget _buttonIcon(
      {required String icon,
      required String title,
      GestureTapCallback? onTap}) {
    return GestureDetector(
        onTap: onTap,
        child: Column(
          children: [
            DirImage(
              url: icon,
              width: 20,
              height: 20,
            ),
            Text(
              title,
              style: const TextStyle(
                  color: Colors.black38,
                  fontSize: 12,
                  decoration: TextDecoration.none),
            )
          ],
        ));
  }

  /// 构建三个按钮
  Widget _buttonVerticalIcons() {
    return Align(
      alignment: Alignment.topRight,
      child: Column(
        children: <Widget>[
          _buttonIcon(icon: Assets.images.icCamera.path, title: '翻转'),
          const SizedBox(
            height: 10,
          ),
          _buttonIcon(icon: Assets.images.bwk.path, title: '倒计时'),
          const SizedBox(
            height: 10,
          ),
          _buttonIcon(icon: Assets.images.c3o.path, title: '闪光灯'),
        ],
      ),
    );
  }

  onTapDialog() {
    Get.defaultDialog(
        title: '温馨提示',
        content: const Text(
          '通过GetX实现弹窗提示',
          style: TextStyle(decoration: TextDecoration.none),
        ),
        onConfirm: () {
          Get.back();
          print('点击了确定');
        });
  }
}
